package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.PerformanceLogs;
import com.example.mall.dto.PerformanceLogsDto;
import com.example.mall.mapper.PerformanceLogsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.PerformanceLogsQuery;
import com.example.mall.service.IPerformanceLogsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 性能日志表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class PerformanceLogsServiceImpl extends ServiceImpl<PerformanceLogsMapper, PerformanceLogs> implements IPerformanceLogsService {

    @Autowired
    private PerformanceLogsMapper performanceLogsMapper;

    @Override
    public PageDTO<PerformanceLogsDto> queryPage(PerformanceLogsQuery performanceLogsQuery) {
        if (performanceLogsQuery == null) {
            performanceLogsQuery = new PerformanceLogsQuery();
        }
        Page<PerformanceLogs> page = performanceLogsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<PerformanceLogs> cartPage = lambdaQuery()
                .like(performanceLogsQuery.getPerformanceId()!= null, PerformanceLogs::getPerformanceId, performanceLogsQuery.getPerformanceId())
                .like(performanceLogsQuery.getModuleName()!= null, PerformanceLogs::getModuleName, performanceLogsQuery.getModuleName())
                .like(performanceLogsQuery.getThroughput()!= null, PerformanceLogs::getThroughput, performanceLogsQuery.getThroughput())
                .like(performanceLogsQuery.getCpuUsage()!= null, PerformanceLogs::getCpuUsage, performanceLogsQuery.getCpuUsage())
                .like(performanceLogsQuery.getMemoryUsage()!= null, PerformanceLogs::getMemoryUsage, performanceLogsQuery.getMemoryUsage())
                .apply(performanceLogsQuery.getResponseTime() != null,
                        "DATE_FORMAT(response_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        performanceLogsQuery.getResponseTime())
                .orderByDesc(PerformanceLogs::getCreatedDate)
                .page(page);
        return PageDTO.of(cartPage, PerformanceLogsDto.class);
    }

    @Override
    public void insert(PerformanceLogsDto performanceLogsDto) {
        PerformanceLogs performanceLogs = new PerformanceLogs();
        CopyUtils.copyProperties(performanceLogsDto, performanceLogs);
        performanceLogs.setPerformanceId(CodeGenerateUtils.generateUniqueId());
        performanceLogs.setCreatedDate(new Date());
        performanceLogsMapper.insert(performanceLogs);
    }

    @Override
    public void update(PerformanceLogsDto performanceLogsDto) {
        AssertUtil.isNotNull(performanceLogsDto.getPerformanceId(), "performanceId不能为空");
        PerformanceLogs performanceLogs = performanceLogsMapper.selectById(performanceLogsDto.getPerformanceId());
        AssertUtil.isNotNull(performanceLogs, "性能日志表信息不存在");
        CopyUtils.copyProperties(performanceLogsDto, performanceLogs);
        performanceLogsMapper.updateById(performanceLogs);
    }

    @Override
    public void delete(Long performanceId) {
        AssertUtil.isNotNull(performanceId, "performanceId不能为空");
        PerformanceLogs performanceLogs = performanceLogsMapper.selectById(performanceId);
        AssertUtil.isNotNull(performanceLogs, "性能日志表信息不存在");
        performanceLogsMapper.deleteById(performanceId);
    }

    @Override
    public void deleteBatch(List<Long> performanceIds) {
        AssertUtil.isNotNull(performanceIds, "performanceIds不能为空");
        List<PerformanceLogs> performanceLogs = performanceLogsMapper.selectBatchIds(performanceIds);
        AssertUtil.isNotNull(performanceLogs, "性能日志表信息不存在");
        performanceLogsMapper.deleteBatchIds(performanceIds);
    }

    @Override
    public PerformanceLogs queryPerformanceLogsById(Long performanceId) {
        AssertUtil.isNotNull(performanceId, "performanceId不能为空");
        return performanceLogsMapper.selectById(performanceId);
    }

    @Override
    public List<PerformanceLogs> queryPerformanceLogsList() {
        return this.list();
    }
}
